package org.alex.netShop.services;

import java.io.IOException;
import java.util.LinkedList;


import org.alex.netShop.beans.CustomerBean;
import org.alex.netShop.beans.ProductBean;
import org.alex.netShop.beans.SellerBean;
import org.alex.netShop.beans.UserBean;
import org.alex.netShop.interfaces.Customer;
import org.alex.netShop.interfaces.Customers;
import org.alex.netShop.interfaces.Product;
import org.alex.netShop.interfaces.Products;
import org.alex.netShop.interfaces.Purchase;
import org.alex.netShop.interfaces.Seller;
import org.alex.netShop.interfaces.Sellers;
import org.alex.netShop.interfaces.Test;
import org.alex.netShop.interfaces.User;
import org.alex.netShop.interfaces.Users;
import org.alex.netShop.service.CustomersBean;
import org.alex.netShop.service.ProductsBean;
import org.alex.netShop.service.Purchases;
import org.alex.netShop.service.PurchasesBean;
import org.alex.netShop.service.SellersBean;
import org.alex.netShop.service.Tests;

import org.alex.netShop.service.TestsBean;
import org.alex.netShop.service.UsersBean;
import org.apache.tapestry5.*;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.OrderedConfiguration;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.Local;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.RequestFilter;
import org.apache.tapestry5.services.RequestHandler;
import org.apache.tapestry5.services.Response;
import org.slf4j.Logger;





public class AppModule
{
    public static void bind(ServiceBinder binder)
    {
        
    }
    
    
    public static void contributeApplicationDefaults(
            MappedConfiguration<String, String> configuration)
    {
        
        
        configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en");

        configuration.add(SymbolConstants.PRODUCTION_MODE, "false");

        configuration.add(SymbolConstants.APPLICATION_VERSION, "1.0-SNAPSHOT");
    }
    

   
    public RequestFilter buildTimingFilter(final Logger log)
    {
        return new RequestFilter()
        {
            public boolean service(Request request, Response response, RequestHandler handler)
                    throws IOException
            {
                long startTime = System.currentTimeMillis();

                try
                {
                    
                    
                    return handler.service(request, response);
                }
                finally
                {
                    long elapsed = System.currentTimeMillis() - startTime;

                    log.info(String.format("Request time: %d ms", elapsed));
                }
            }
        };
    }

   
    public void contributeRequestHandler(OrderedConfiguration<RequestFilter> configuration,
            @Local
            RequestFilter filter)
    {
        
        configuration.add("Timing", filter);
    }
    
    public static Customers buildCustomers(){
    	Customers customers = new CustomersBean();
    	Customer customer = new CustomerBean();
    	customer.setName("Marko");
    	customer.setSurname("Markovic");
    	customer.setDescription("");
    	customer.setPurchase(new LinkedList<Purchase>());
    	customers.save(customer);
    	return customers;
    }
    public static Products buildProducts(){
    	Products products = new ProductsBean();
    	Product product = new ProductBean();
    	product.setName("hard disk");
    	product.setPrice(40);
    	
    	product.setPurchase(new LinkedList<Purchase>());
    	products.save(product);
    	return products;
    }
    public static Sellers buildSellers(){
    	Sellers Sellers = new SellersBean();
    	Seller Seller = new SellerBean();
    	Seller.setName("Aleksandra");
    	Seller.setSurname("Vemic");
    	
    	Seller.setPurchase(new LinkedList<Purchase>());
    	Sellers.save(Seller);
    	return Sellers;
    }
    public static Purchases buildPurchases() {
		return new PurchasesBean();
	}   
    public static Users buildUsers() {

		Users users = new UsersBean();
		User userlog = new UserBean();

		userlog.setUserName("saska");
		userlog.setPassword("saska");

		users.save(userlog);
		return users;
	}
    
    public static Tests buildTests() {
		return new TestsBean();
	}   
    
  
  

}
